package net.hn.hnms.biz.training.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import net.hn.hnms.biz.training.mapper.StatisticMapper;
import net.hn.hnms.biz.training.service.IStatisticService;
import net.hn.hnms.biz.training.vo.StatisticVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ly
 * 统计服务层
 * @date 2022-08-26
 */
@Service
public class StatisticServiceImpl implements IStatisticService {


    @Autowired
    private StatisticMapper statisticMapper;

    /**
     *
     * @return List<StatisticVO>
     */
    @Override
    public List<StatisticVO> monthStatistic(String mineCode) {
        Date now = new Date();
        int month = DateUtil.month(now) + 1;
        List<StatisticVO> list = statisticMapper.monthStatistic(mineCode);

        List<StatisticVO> result = new ArrayList<>(month);
        for (int i = 1; i < month + 1; i++) {
            int finalI = i;
            StatisticVO statisticVO = list.stream().filter(o -> Convert.toInt(o.getMonth(), 0) == finalI).findFirst().orElse(null);
            if (statisticVO != null) {
                result.add(statisticVO);
            }else{
                result.add(new StatisticVO(i, 0L, 0L));
            }
        }
        return result;
    }

}
